package com.lane.sidecn.product.web;

import org.springframework.beans.BeanUtils;

import com.lane.core.web.BaseController;
import com.lane.sidecn.product.model.Product;

/**
 * @author 张先宙 E-mail: zhangxianzhou_std@163.com
 * @version 1.0 创建时间：Jun 30, 2009 10:37:50 PM
 * 
 */

public class ProductController extends BaseController {

	private Product product;

	/**
	 * 添加新商品
	 */
	public void add() {

		if (product == null) {
			printMessage("addProductFail");// 添加失败
		}
		try {
			baseManager.save(product);// 保存新商品
			printMessage("addProductSuccess");
		} catch (Exception e) {
			printMessage("addProductFail");// 保存失败
		}

	}

	/**
	 * 通过名字移除已有商品
	 */

	public void remove() {

		if (product == null) {
			printMessage("removeProductFail");// 移除商品失败
		}
		Product removeProduct = findByName();// 通过名字找到商品
		try {
			baseManager.remove(removeProduct);// 移除商品
			printMessage("removeProductSuccess");
		} catch (Exception e) {
			printMessage("removeProductFail");// 移除失败
		}
	}

	/**
	 * 通过名字找到商品对象
	 */
	public Product findByName() {

		Product tproduct = baseManager.findUniqueBy(Product.class, "name", product.getName());

		return tproduct;
	}

	/**
	 * 更新商品信息
	 */
	public void update() {
		Product oldProduct = findByName();
		BeanUtils.copyProperties(oldProduct, product);
		try {
			baseManager.save(product);// 保存更新信息
			printMessage("addProductSuccess");
		} catch (Exception e) {
			printMessage("addProductFail");// 保存失败
		}

	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
}
